import asyncio
import time

def download_file(filename):
    print(f"开始下载文件：{filename}")
    time.sleep(3)   # 模拟下载文件耗时
    print(f"下载文件：{filename}完成")

def sync_download():
    start_time = time.time()
    print("开始同步下载文件。。。")
    download_file("file1.txt")
    download_file("file2.txt")
    download_file("file3.txt")
    print("同步下载完成")
    print(f"同步下载耗时：{time.time() - start_time:.2f}秒")


async def download_file_async(filename):
    print(f"开始下载文件：{filename}")
    await asyncio.sleep(3)  # 模拟异步下载需要3秒
    print(f"下载完成：{filename}")

async def async_download():
    start_time = time.time()
    print("开始异步下载文件。。。")
    tasks = [download_file_async(filename) for filename in ["file1.txt", "file2.txt", "file3.txt"]]
    await asyncio.gather(*tasks)
    print("异步下载完成")
    print(f"异步下载耗时：{time.time() - start_time:.2f}秒")

if __name__ == '__main__':
    asyncio.run(async_download())   # 运行异步任务,总耗时是：3.01秒
    print("=" * 30)
    sync_download() # 同步任务，总耗时是：9秒
